<?php
class CollectionsController extends AppController {

	var $name = 'Collections';
	var $helpers = array('Html', 'Form', 'Javascript', 'Accesscount');
	var $uses = array('LedaCollection', 'Collection', 'Accesscount');
	var $paginate = array('limit' => 5000, 'recursive' => 1);

	function beforeFilter(){
		parent::beforeFilter();

		if ( empty($this->passedArgs['col_id']) ) { // col_id が空ならエラー
			$this->redirect(array('controller'=>'pages','action'=>'error'));
		}

		$arrCollectionIdMax = $this->Collection->find('first', array('fields' => 'MAX(collection_id)'));
		if ( empty($arrCollectionIdMax[0]['max']) ) { // collection数が取得出きない場合はエラー
			$this->redirect(array('controller'=>'pages','action'=>'error'));
		}

		if ( intval( $this->passedArgs['col_id'] ) > $arrCollectionIdMax[0]['max'] ) { // collection数より大きい数値はエラー
			$this->redirect(array('controller'=>'pages','action'=>'error'));
		}

		if( !empty( $this->passedArgs['col_id']) ) { // col_id をサニタイズする
			$this->passedArgs['col_id'] = intval($this->passedArgs['col_id']);
		}
	}

	function index() {
		// コレクションID
		$collectionId = $this->passedArgs['col_id'];

		// 一覧表示用
		$query = array('conditions' => array("collection_id" => $collectionId) );
		$this->paginate = array_merge($this->paginate, $query);

		// 検索タイトル表示用
		$name = $this->Collection->find('first', array('conditions' => array("collection_id" => $collectionId)));
		$name = empty($name['Collection']['name']) ? '' : $name['Collection']['name'];

		$this->set('name', $name);
		$this->set('ledaCollections', $this->paginate('LedaCollection'));
		$this->set('accesslogs', $this->Accesscount->_get_accesslog()); // アクセスログ
	}
}
?>